<template>
	<div class="export-dialog-view" v-loading="pageLoading">
		<div class="export-dialog">
			<div class="left-lay">
				<div class="split-title">{{ title }}</div>
				<div class="left-line">
					<div class="line-title">
						{{ $t("message.bhView.leftComponents.device") }}
					</div>
					<div class="line-value">{{ $store.state.btn_set_BHView2 ? $store.state.plantName : '担当課' }}</div>
				</div>


				<div class="left-line">
					<div class="line-title">
						{{ $t("message.bhView.leftComponents.object") }}
					</div>
					<div class="line-value">
						{{
							$store.state.macType == 1
								? $t("message.bhView.leftComponents.machine")
								: $t("message.bhView.leftComponents.piping")
						}}
					</div>
				</div>

				<div class="left-line">
					<div class="line-title">
						{{ $t("message.reportFrom.exportDialog.title_c") }}
					</div>
					<div class="line-value">PDFで出力</div>
				</div>

				<div class="left-draw" id="left-draw"></div>
			</div>

			<div class="tip-lay"></div>

			<div class="right-lay">
				<div class="split-title text-right">
					<i class="el-icon-close" @click="CloseExprotDiloag"></i>
				</div>
				<div class="split-title">
					{{ $t("message.reportFrom.exportDialog.title_l") }}
				</div>



				<div class="right-line">
					<div class="line-title">
						用紙サイズ
					</div>
					<div class="line-content">
						<el-select v-model="Data.PaperSize" placeholder=" " class="ipt">
							<el-option v-for="(item, index) in ComList.paperSize" :key="item.key" :label="item.label"
								:value="item.key">
							</el-option>
						</el-select>
					</div>
				</div>
				<div class="right-line">
					<div class="line-title">
						用紙方向
					</div>
					<div class="line-content" style="background-color: #efeded;padding: 4px 4px 4px 0;">
						<el-radio v-model="Data.PaperDirection" :label="0">縦</el-radio>
						<el-radio v-model="Data.PaperDirection" :label="1">横</el-radio>
					</div>
				</div>
				<div class="right-line">
					<div class="line-title">
						図面種類
					</div>
					<div class="line-content" style="background-color: #efeded;padding: 4px 4px 4px 0;"
						@change="Data.ThickInspForPro = 0">
						<el-radio v-model="Data.ClsType" :label="0">部品</el-radio>
						<el-radio v-model="Data.ClsType" :label="1">検査</el-radio>
						<el-radio v-model="Data.ClsType" :label="2">肉厚</el-radio>
					</div>
				</div>
				<div class="right-line">
					<div class="line-title">
						表示項目
					</div>
					<div class="line-content">
						<el-radio v-model="Data.ShowProject" :label="0">図面のみ</el-radio>
					</div>
				</div>
				<div class="right-line">
					<div class="line-title">

					</div>
					<div class="line-content">
						<el-radio v-model="Data.ShowProject" :label="1" :disabled="Data.ClsType == 0">バルーン</el-radio>
						<el-select v-model="Data.Balloon" placeholder=" " class="ipt" style="width: 158px;"
							:disabled="Data.ClsType == 0 || Data.ShowProject != 1">
							<el-option v-for="(item, index) in ComList.balloon" :key="item.key" :label="item.label"
								:value="item.key">
							</el-option>
						</el-select>
					</div>
				</div>
				<div class="right-line">
					<div class="line-title">

					</div>
					<div class="line-content">
						<el-radio v-model="Data.ShowProject" :label="2"
							:disabled="Data.ClsType == 0 || Data.ClsType == 2">マークアップ</el-radio>
					</div>
				</div>
				<div class="right-line">
					<div class="line-title">
						検査工事項目
					</div>
					<div class="line-content">
						<el-select v-model="Data.ThickInspForPro" placeholder=" " class="ipt"
							:disabled="Data.ClsType == 0 || (Data.ClsType == 1 && Data.ShowProject != 1) || (Data.ClsType == 2 && Data.ShowProject != 1)">
							<el-option
								v-for="(item, index) in Data.ClsType == 1 && Data.ShowProject == 1 ? ComList.inspForPro_Ins : ComList.thickInspForPro"
								:key="item.key" :label="item.label" :value="item.key">
							</el-option>
						</el-select>
					</div>
				</div>
				<div class="right-line">
					<div class="line-title">
						検査工事目的
					</div>
					<div class="line-content">
						<el-select v-model="Data.InspForPurpose" placeholder=" " class="ipt"
							:disabled="Data.ClsType == 0 || Data.ClsType == 1 || (Data.ClsType == 2 && Data.ShowProject != 1)">
							<el-option v-for="(item, index) in ComList.inspForPurpose" :key="item.key"
								:label="item.label" :value="item.key">
							</el-option>
						</el-select>
					</div>
				</div>
				<div class="right-line">
					<div class="line-title">
						時期指定
					</div>
					<div class="line-content">
						<el-date-picker type="month" :format="format" :value-format="format" placeholder=" "
							class="ipt picker orange" v-model="Data.From"
							:disabled="!(Data.ClsType == 2 && Data.ShowProject == 1) || Data.ExProject == 0">
						</el-date-picker>
						<span>~</span>
						<div class="line-content">
							<el-date-picker type="month" :format="format" :value-format="format" placeholder=" "
								class="ipt picker orange" v-model="Data.To"
								:disabled="!(Data.ClsType == 2 && Data.ShowProject == 1) || Data.ExProject == 0">
							</el-date-picker>
						</div>
					</div>
				</div>
				<div class="right-line">
					<div class="line-title">
						出力対象
					</div>
					<div class="line-content">
						<el-radio v-model="Data.ExProject" :label="0"
							:disabled="!(Data.ClsType == 2 && Data.ShowProject == 1)">全測定点</el-radio>
					</div>
				</div>
				<div class="right-line">
					<div class="line-title">

					</div>
					<div class="line-content">
						<el-radio v-model="Data.ExProject" :label="1"
							:disabled="!(Data.ClsType == 2 && Data.ShowProject == 1)">計画箇所</el-radio>
					</div>
				</div>
				<div class="right-line">
					<div class="line-title">

					</div>
					<div class="line-content">
						<el-radio v-model="Data.ExProject" :label="2"
							:disabled="!(Data.ClsType == 2 && Data.ShowProject == 1)">未計画箇所</el-radio>
					</div>
				</div>
				<div class="right-line">
					<div class="line-title">

					</div>
					<div class="line-content">
						<el-checkbox v-model="Data.Inversion"
							:disabled="!(Data.ClsType == 2 && Data.ShowProject == 1) || Data.ExProject == 0 || Data.ExProject == 2">計画バルーン反転</el-checkbox>
					</div>
				</div>
			</div>
			<div class="FIX">
				<getSvgView v-show="true" ref="cadViewForBH" @changeWait="changeWait" :selectMacData="selectMacData"/>
			</div>
			<footer class="footer-view">
				<div class="">
					<div class="line-title" style="width: 100%">
						{{ $t("message.reportFrom.exportDialog.tip") }}
					</div>
				</div>
				<div>
					<button @click="CloseExprotDiloag">Cancel</button>
					<button @click="onExportClick">OK</button>
				</div>
			</footer>
		</div>
	</div>
</template>

<script>
import {
	ExCGInspInventoryCombox, ExWithoutFrameDrawing, ExPDFWithoutFrameDrawing

} from "@/api/api";
import getSvgView from "./getSvg.vue";
export default {
	props: ["title", "fileTypeStr", "selectMacData", "fileRadio"],
	name: "B_B_Export",
	data() {
		return {
			pageLoading: false,
			ComList: [],
			Data: {

				PaperSize: 0,
				PaperDirection: 0,

				InspForPurpose: 1,
				ThickInspForPro: 0,
				ClsType: 0,
				ShowProject: 0,
				Balloon: 0,
				From: '',
				To: '',
				ExProject: 0,
				Inversion: false,
				// ClsType: 0,
				// ClsType: 0,

			},
			format: 'yyyy-MM',
			flg: true
		};
	},
	components: { getSvgView },
	watch: {},
	methods: {
		CloseExprotDiloag() {
			this.$emit("CloseExprotDiloag");
		},
		changeWait(e, flg) {
			this.state = e;
			this.flg = flg;
		},
		wait() {
			return new Promise((resolve, reject) => {
				if (this.state) {
					resolve();
				} else {
					const intervalId = setInterval(() => {
						if (this.state) {
							clearInterval(intervalId);
							resolve();
						}
					}, 100);
				}
			});
		},
		async onExportClick() {
			let MacNumb = [];
			let DmgCode = [];
			let svgObj = [];

			for (let index = 0; index < this.selectMacData.length; index++) {
				let i = this.selectMacData[index];
				this.flg = false;
				let tempJataJson = {
					PltCode: i.pltCode,
					MacType: i.macType,
					MacNumb: i.macName,
					DmgCode: [i.macCode],
					...this.Data,
				}

				console.log('tempJataJson', tempJataJson);
				this.pageLoading = true;
				this.$refs.cadViewForBH.getSvgByFunFn(
					tempJataJson
				);
				this.state = false;
				await this.wait();
				// 当接口报错，且只有一个机器时，给提示，并终止
				if (this.flg && this.selectMacData.length == 1) {
					this.pageLoading = false;
					return
				}
				let svgHtml = ''
				if (!this.flg) {
					svgHtml = await this.$refs.cadViewForBH.exportSvgDataStr();
				}

				let temp = {
					dmgCode: i.macCode,
					svgHtml
				}
				if (this.flg) {
					temp.svgHtml = '';
					this.pageLoading = false;
					MacNumb.push(i.macName);
					DmgCode.push(i.macCode);
					svgObj.push(temp);
					this.state = false;
					continue
				}
				

				MacNumb.push(i.macName);
				DmgCode.push(i.macCode);
				svgObj.push(temp);
				this.state = false;
			}

			this.pageLoading = true;
			let dataJson = {
				PltCode: this.selectMacData[0].pltCode,
				MacType: this.selectMacData[0].macType,
				MacNumb,
				DmgCode,
				svgObj,
				...this.Data,
			}
			dataJson.PlanFlag = Number(dataJson.PlanFlag);
			console.log('dataJson', dataJson);
			let res = await ExPDFWithoutFrameDrawing(dataJson);
			this.pageLoading = false;

			if (!res) {
				return
			}
			if (res.data.code == 451) {
				this.$alert(res.data.msg, '', {
					confirmButtonText: 'OK',
					type: 'warning',
				});
				return
			}
			if (res.data.code != 0) {
				this.$alert(res.data.msg, '', {
					confirmButtonText: 'OK',
					type: 'warning',
				});
				return
			}
			// this.$message.success(res.data.msg);
			this.$emit("CloseExprotDiloag");
			window.open(res.data.data);

			return
		},


		// 获取下拉框
		async ExCGInspInventoryComboxFn() {
			this.pageLoading = true;
			let res = await ExCGInspInventoryCombox({ pltCode: this.selectMacData[0].pltCode });
			setTimeout(() => {
				this.pageLoading = false;
			}, 300);
			this.Data.From = res.data.data.meishiPeriodFrom;
			this.Data.To = res.data.data.meishiPeriodTo;

			this.ComList = res.data.data;

		},
		// 开始
		async initFn() {
			// // 获取当前日期  
			// const now = new Date();

			// // 获取当前年份  
			// const currentYear = now.getFullYear();

			// // 获取当前月份（注意：JavaScript的月份是从0开始的，所以我们需要+1）  
			// const currentMonth = now.getMonth() + 1;

			// // 格式化当前年月  
			// const currentYearMonth = `${currentYear}-${currentMonth < 10 ? '0' + currentMonth : currentMonth}`;

			// this.Data.From = currentYearMonth;
			// console.log('当前年月:', currentYearMonth);

			// // 计算十一个月后的年月  
			// const elevenMonthsLater = new Date(now.getFullYear(), now.getMonth() + 11, 1); // 设置日期为1号，因为月份可能会改变  

			// // 获取十一个月后的年份  
			// const futureYear = elevenMonthsLater.getFullYear();

			// // 获取十一个月后的月份（同样，需要+1并格式化）  
			// const futureMonth = elevenMonthsLater.getMonth() + 1;

			// // 格式化十一个月后的年月  
			// const futureYearMonth = `${futureYear}-${futureMonth < 10 ? '0' + futureMonth : futureMonth}`;
			// this.Data.To = futureYearMonth;
			// console.log('十一个月后的年月:', futureYearMonth);
			this.ExCGInspInventoryComboxFn();

		}
	},
	created() {


	},
	mounted() {
		this.initFn();
	},
	beforeDestroy() { },
	destroyed() { },
};
</script>

<style lang="scss" scoped>
.export-dialog-view {
	position: fixed;
	width: 100%;
	height: 100%;
	left: 0;
	top: 0;
	z-index: 6;
}

.export-dialog {
	position: absolute;
	top: 50%;
	left: 50%;
	transform: translate(-50%, -50%);
	width: 680px;
	height: 650px;
	// height: 292px;
	background: #ffffff;
	box-shadow: 0px 12px 48px 16px rgba(0, 0, 0, 0.05),
		0px 9px 28px 0px rgba(0, 0, 0, 0.08),
		0px 6px 16px -8px rgba(0, 0, 0, 0.15);
	display: flex;
	flex-direction: row;
	padding: 0 12px;
}

.left-lay {
	position: relative;
	width: 30%;
	height: 100%;
	// border-right: 1px solid #ccc;
	display: flex;
	flex-direction: column;
	// background-color: blanchedalmond;

	.left-line {
		width: 100%;
		display: flex;
		flex-direction: row;
		align-items: center;
		padding: 0px 20px;
		padding-top: 10px;

		.line-title {
			font-size: 14px;
			font-weight: 700;
			color: rgba(0, 0, 0, 0.85);
			line-height: 22px;
		}

		.line-value {
			font-size: 14px;
			font-weight: 400;
			color: #7b7b7b;
			line-height: 22px;
			margin-left: 10px;
		}
	}

	.left-draw {
		position: absolute;
		top: 100vh;
		width: 750px;
		height: 611px;
		// flex-grow: 1;
		// flex-shrink: 1;
		background-color: bisque;
	}
}

.tip-lay {
	background: red;
	width: 7px;
	border-radius: 4px;
	background: #ccc;
	margin-top: 46px;
	margin: 46px 15px 85px 15px;
}

.right-lay {
	position: relative;
	// width: 60%;
	height: 100%;
	display: flex;
	flex-direction: column;

	.el-icon-close {
		font-size: 16px;
		cursor: pointer;
		width: 46px;
		height: 45px;
		line-height: 45px;
	}

	.right-block-container {
		width: 100%;
		display: flex;
		flex-direction: column;
	}

	.text-right {
		padding: 0 !important;
		text-align: right;
	}

	.right-line {
		width: 100%;
		display: flex;
		flex-direction: row;
		align-items: center;
		padding: 0px 12px;
		padding-top: 10px;

		.line-title {
			min-width: 100px;
			font-size: 14px;
			font-weight: 700;
			color: rgba(0, 0, 0, 0.85);
			line-height: 22px;
			text-align: left;
			margin-right: 10px;
		}

		.line-content {
			// flex-grow: 1;
			// flex-shrink: 1;
			display: flex;
			flex-direction: row;
			align-items: center;
		}

		.ipt {
			//     width: 368px;
			width: 268px;
			height: 32px;

			::v-deep .el-input__inner {
				width: 100%;
				height: 32px;
			}

			::v-deep .el-input__inner:focus {
				border: 1px solid #dcdfe6;
			}

			::v-deep .el-input__icon {
				line-height: 32px;
				color: #909399;
			}

			::v-deep .el-input__suffix {
				width: 32px;
				right: 0;
				background: #dee0e3;
			}
		}

		.picker {
			width: 119px;
			// margin: 0px 10px;

			::v-deep .el-input__inner {
				width: 100%;
			}

			::v-deep .el-input__suffix {
				background: transparent;
			}

		}

		span {
			padding: 0 5px;
			margin: 0 5px;
		}

		.divider {
			width: 100%;
			height: 1px;
			background-color: #f0f0f0;
		}
	}

	.right-line.bottom-border {
		padding-bottom: 10px;
		// border-bottom: 1px solid #f0f0f0;
	}

	.flex-row {
		flex-direction: row;
	}

	.flex-column {
		flex-direction: column;
	}
}

.split-title {
	width: 100%;
	font-size: 16px;
	font-weight: 700;
	color: rgba(0, 0, 0, 0.85);
	line-height: 22px;
	padding: 12px 12px;
	border-bottom: 1px solid #f0f0f0;
}

.footer-view {
	width: calc(100% - 30px);
	box-sizing: border-box;
	position: absolute;
	z-index: 10;
	bottom: 16px;
	// padding: 0 15px;
	display: flex;
	flex-direction: row;
	align-items: center;
	justify-content: space-between;
	// background-color: red;
	padding-top: 17px;
	border-top: 1px solid #f0f0f0;
	font-size: 16px;
	font-weight: 700;

	button {
		width: 82px;
		height: 32px;
		background: #ffffff;
		border: 1px solid #d9d9d9;
		margin-left: 12px;
		border-radius: 4px;
		font-size: 14px;
		font-weight: 500;
	}

	button:nth-child(2) {
		background: #106ebe;
		color: #fff;
	}
}

::v-deep .el-radio {
	min-width: 60px;
}



.FIX {
	width: 750px;
	height: 611px;
	position: fixed;
	top: 4000px;
	left: -500px;
}
</style>
